package controllerlayer;

import java.util.ArrayList;

import modellayer.Company;
import modellayer.CompanyType;
import modellayer.KeyPerson;
import modellayer.Service;
import modellayer.ZipCode;
import dblayer.CompanyDB;
import dblayer.CompanyTypeDB;
import dblayer.ServiceDB;

/**
 * The CompanyCtrl class to handle Company information
 * @author Allan Eriksen, Andreas Rye, Casper Madsen, Christian Stidsborg, Stefan Bols
 */

public class CompanyCtrl 
{
	private CompanyDB companyDB;
	private ServiceDB serviceDB;
	private CompanyTypeDB companyTypeDB;
	/**
	 * @param companyDB
	 */
	
	/**
	 * Constructor for the companyCtrl class
	 */
	public CompanyCtrl()
	{
		companyDB = new CompanyDB();
		serviceDB = new ServiceDB();
		companyTypeDB = new CompanyTypeDB();
	}
	/**
	 * Method for finding company by using CVR
	 * @param cvr
	 * @return result
	 */
	public Company findCompanyByCvr(int cvr)
	{
		Company result = companyDB.findCompanyByCvr(cvr);
		return result;
	}
	
	public ArrayList<Company> getAllCompanies() {
		return companyDB.getCompanies(null);
	}
	
	public ArrayList<Company> listOfAllCompany()
	{
		return companyDB.listAllCompanies();
	}
	
	public boolean deleteCompany(int cvr) throws Exception {
		 return companyDB.deleteCompany(cvr);
	}
	
	public boolean createCompany(int cvr, String name, CompanyType companyType, String address, ZipCode zipCode, String mainPhone, String mainMail, String website, Service mainService, Service secService, int noOfEmployees, long addedDate) {
		boolean success = false;

		Company c = new Company(cvr, name, companyType, address, zipCode, mainPhone, mainMail, website, mainService, secService, noOfEmployees, addedDate);

		try {
			if (companyDB.insertCompany(c) != -1) {
				success = true;
			}
		} catch (Exception e) {
			System.out
					.println("Failed to insert company into database! (CompanyCtrl) Error: "
							+ e);
		}

		return success;
	}
	
	public ArrayList<CompanyType> listOfAllTypes()
	{
		return companyTypeDB.listAllTypes();
	}
	
	public ArrayList<Service> listOfAllServices()
	{
		return serviceDB.listAllServices();
	}
}
